/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import at.socialconference.app.model.AConference;
import at.socialconference.app.model.APlace;
import at.socialconference.app.uihelper.AlertHelper;
import at.socialconference.inter.connection.protocol.SocketProtocol;

public class ConferenceActivity extends Activity {

	private AConference con;
	
	private TextView tvName, tvDate, tvLocation, tvDescription;
	private Button btnEnroll;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conferenceview);
		
		Bundle b = getIntent().getExtras();
		con = (AConference)b.getParcelable("conference");
		
		tvName = (TextView)findViewById(R.id.cv_trname);
		tvDate = (TextView)findViewById(R.id.cv_trdate);
		tvLocation = (TextView)findViewById(R.id.cv_trlocation);
		tvDescription = (TextView)findViewById(R.id.cv_trdescription);
		
		btnEnroll = (Button)findViewById(R.id.cv_btnEnroll);
		
		tvName.setText(con.getName());
		APlace pl = con.getPlace();
		if(pl!=null)
			tvLocation.setText(pl.getName());
		else
			tvLocation.setText("???");
		
		String fromTo;
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");
		fromTo = sdf.format(con.getDate_from())+" - "+sdf.format(con.getDate_to());
		tvDate.setText(fromTo);
		tvName.setText(con.getName());
		tvDescription.setText(con.getDescription());
		
		if(con.isEnrolled()){
			btnEnroll.setVisibility(View.GONE);
		}else{
			btnEnroll.setOnClickListener(new OnClickListener() {	
				@Override
				public void onClick(View v) {
					int retVal = con.enroll();
					if(retVal==SocketProtocol.OK || retVal == SocketProtocol.RESP_ALREADYENROLLED){
						btnEnroll.setVisibility(View.GONE);
					}else if(retVal==SocketProtocol.RESP_FAILURE){
						AlertHelper.alert("Failure", "A failure appeared", false, ConferenceActivity.this);
					}
				}
			}
			);
		}
		
		
	}
}
